<template>
  <div class="welcome-page">
    <header>
      <a href="#" class="logo">
        <img src="@/assets/logo.png" alt="商城Logo" class="logo-img">
      </a>
      <ul>
        <li><a href="#" class="active">首页</a></li>
        <li><a href="#sec">关于</a></li>
      </ul>
    </header>
    <section>
      <img src="@/assets/stars.png" id="stars">
      <img src="@/assets/moon.png" id="moon">
      <img src="@/assets/mountains_behind.png" id="mountains_behind">
      <h2 id="text">Aura Gift</h2>
      <a href="javascript:void(0)" id="nes-btn" @click="goToLogin">点击进入</a>
      <img src="@/assets/mountains_front.png" id="mountains_front">
    </section>
    <div class="sec" id="sec">
      <div class="title-container">
        <span class="first">小</span><span>礼</span><span>有</span><span>大</span><span>心</span><span class="first">， </span><span>定</span><span  class="first">制</span><span>专</span><span>属</span><span>情</span><span>感</span>
      </div>
      <br><br>
      <div class="welcome-text" ref="welcomeTextRef">
        <p v-for="(text, index) in welcomeTexts" 
           :key="index" 
           :class="{animate: showWelcomeTexts[index]}" 
           :ref="el => paragraphRefs[index] = el"
           class="welcome-paragraph">
          {{ text }}
        </p>
      </div>
      
      <!-- 花朵组件 -->
      <div class="flowers-group" :class="{show: showFlower}" ref="flowerRef">
        <div class="flower">
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="rod">
            <div class="spike"></div>
            <div class="spike"></div>
            <div class="spike"></div>
            <div class="spike"></div>
          </div>
        </div>
        <div class="flower">
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="rod">
            <div class="spike"></div>
            <div class="spike"></div>
            <div class="spike"></div>
            <div class="spike"></div>
          </div>
        </div>
        <div class="flower">
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="petal"></div>
          <div class="rod">
            <div class="spike"></div>
            <div class="spike"></div>
            <div class="spike"></div>
            <div class="spike"></div>
          </div>
        </div>
        
        <!-- Logo GIF -->
        <div class="logo-gif-container" :class="{show: showLogo}">
          <img src="@/assets/logo.gif" alt="Logo Animation" class="logo-gif">
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const showWelcomeTexts = ref([false, false, false, false, false, false])
const showFlower = ref(false)
const showLogo = ref(false)
const welcomeTextRef = ref(null)
const flowerRef = ref(null)
const paragraphRefs = ref([])
let observer = null
let flowerObserver = null
let logoObserver = null

const welcomeTexts = [
  '当挚友的生日临近，当爱人的纪念日就在眼前，当教师节、母亲节不期而至，您打开购物网站，在琳琅满目的商品中反复翻找，内心却充满了犹豫与不确定——送一款香水？似乎不够独特；送一个杯子？又恐流于俗套。您深知，礼物不仅仅是物品，它更是一份心意的载体，是无声的语言，承载着感谢、爱意与祝福。然而，找到那件能精准代言您心意的礼物，竟成了一场耗费心力的挑战。',
  '我们深知您的烦恼。在标准化生产的时代，那些千篇一律的礼物，如何能承载您独一无二的情感？您想要的，不是最昂贵的，而是最"对"的；不是简单的物质交换，而是一次深刻的情感连接。',
  '这正是我们存在的意义。',
  '我们致力于打造一个专注于场景化情感定制的礼品服务平台。我们相信，每一个值得纪念的时刻，都值得拥有一份为其量身打造的独特献礼。在这里，您无需再大海捞针。我们为您精心构思了覆盖生日、情人节、教师节、纪念日等全场景的礼品方案，每一款都不是商品的简单堆砌，而是一个个完整的"心意故事"。',
  '您可以轻松找到为"科幻迷男友"准备的【星际漫游】探索套装，内含定制星球仪与陨石主题书签；也能发现为"引路恩师"精心设计的【春风化雨】感恩礼盒，其中刻有姓名的黄铜镇纸与寄语茶包，皆在诉说桃李之情。若您有更独特的想法，我们的深度定制服务将成为您的专属礼宾——您提供创意与故事，我们负责将其淬炼成一件触手可及的艺术品，无论是镌刻密语的饰品，还是印刻共同记忆的画卷，我们都将让它成为无可替代的情感信物。',
  '让我们为您解决"送什么"的难题，将您宝贵的时间与心意，投入到更温暖的陪伴与交流中去。登录「Aura Gift——心意造物」，让每一份深情，都有迹可循；让每一个重要的人，都感受到您独一无二的心意。'
]

const goToLogin = () => {
  router.push('/login')
}

onMounted(() => {
  // 视差滚动效果
  let stars = document.getElementById('stars')
  let moon = document.getElementById('moon')
  let mountains_behind = document.getElementById('mountains_behind')
  let text = document.getElementById('text')
  let btn = document.getElementById('nes-btn')
  let mountains_front = document.getElementById('mountains_front')
  let header = document.querySelector('header')

  window.addEventListener('scroll', function(){
    let value = window.scrollY;
    if (stars && moon && mountains_behind && mountains_front && text && btn && header) {
      stars.style.left = value * 0.25 + 'px'
      moon.style.top = value * 1.05 + 'px'
      mountains_behind.style.top = value * 0.5 + 'px'
      mountains_front.style.top = value * 0 + 'px'
      text.style.marginRight = value * 4 + 'px'
      text.style.marginTop = value * 1.5 + 'px'
      btn.style.marginTop = value * 1.5 + 'px'
      header.style.top = value * 0.5 + 'px'
    }
  })

  // 使用Intersection Observer观察每个段落是否可见
  if (welcomeTextRef.value) {
    observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        const index = parseInt(entry.target.dataset.index)
        if (entry.isIntersecting) {
          // 延迟显示，让段落依次出现
          setTimeout(() => {
            showWelcomeTexts.value[index] = true
          }, index * 500) // 每个段落延迟500ms显示
        }
      })
    }, {
      threshold: 0.2, // 当20%的元素可见时触发
      rootMargin: '0px' // 没有边距调整
    })
    
    // 观察每个段落
    paragraphRefs.value.forEach((el, index) => {
      if (el) {
        el.dataset.index = index
        observer.observe(el)
      }
    })
  }

  // 花朵触发
  if (welcomeTextRef.value) {
    flowerObserver = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          // 当welcomeTexts区域进入视窗时，立即显示花朵
          showFlower.value = true
        }
      })
    }, {
      threshold: 0.2, // 当20%的welcomeTexts区域可见时触发
      rootMargin: '0px'
    })
    
    flowerObserver.observe(welcomeTextRef.value)
  }

  // Logo延迟触发
  if (welcomeTextRef.value) {
    logoObserver = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          // 当welcomeTexts区域进入视窗时，延迟显示Logo
          setTimeout(() => {
            showLogo.value = true
          }, 2000) // 延迟2秒显示Logo
        }
      })
    }, {
      threshold: 0.3, // 当30%的welcomeTexts区域可见时触发
      rootMargin: '0px'
    })
    
    logoObserver.observe(welcomeTextRef.value)
  }

  // 文字效果
  let spans = document.querySelectorAll('.title-container span');
  for (let i = 0; i < spans.length; i++) {
    spans[i].addEventListener("mouseover", function() {
      this.classList.add('flat');

      if (i % 2 == 0) {
        if (spans[i + 1]) spans[i + 1].classList.add('flat');
      } else {
        if (spans[i - 1]) spans[i - 1].classList.add('flat');
      }
    });

    spans[i].addEventListener("mouseout", function() {
      this.classList.remove('flat');

      if (i % 2 == 0) {
        if (spans[i + 1]) spans[i + 1].classList.remove('flat');
      } else {
        if (spans[i - 1]) spans[i - 1].classList.remove('flat');
      }
    });
  }
})

// 组件卸载前清理observer
onBeforeUnmount(() => {
  if (observer && welcomeTextRef.value) {
    observer.unobserve(welcomeTextRef.value)
    observer = null
  }
  if (flowerObserver && welcomeTextRef.value) {
    flowerObserver.unobserve(welcomeTextRef.value)
    flowerObserver = null
  }
  if (logoObserver && welcomeTextRef.value) {
    logoObserver.unobserve(welcomeTextRef.value)
    logoObserver = null
  }
})
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.welcome-page {
  background: linear-gradient(#2b1055, #7597de);
  min-height: 100vh;
  overflow-x: hidden;
}

header {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  padding: 30px 100px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 10000;
}

header .logo {
  color: #fff;
  font-weight: 700;
  text-decoration: none;
  font-size: 2em;
  text-transform: uppercase;
  letter-spacing: 2px;
}

header ul {
  display: flex;
  justify-content: center;
  align-items: center;
}

header ul li {
  list-style: none;
  margin-left: 20px;
}

header ul li a {
  text-decoration: none;
  padding: 6px 15px;
  color: #fff;
  border-radius: 20px;
}

header ul li a:hover,
header ul li a.active {
  background: #fff;
  color: #2b1055;
}

section {
  position: relative;
  width: 100%;
  height: 100vh;
  padding: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

section img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

section img#moon {
  mix-blend-mode: screen;
}

section img#mountains_front {
  z-index: 10;
}

#text {
  position: absolute;
  color: #fff;
  white-space: nowrap;
  font-size: 7.5vw;
  z-index: 9;
  transform: translateY(-100px);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

#nes-btn {
  display: inline-block;
  padding: 8px 30px;
  background: #fff;
  color: #2b1055;
  border-radius: 40px;
  font-size: 1.5em;
  z-index: 9;
  transform: translateY(100px);
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease;
}

#nes-btn:hover {
  background: #2b1055;
  color: #fff;
  transform: translateY(100px) scale(1.1);
}

.sec {
  position: relative;
  padding: 100px;
  background: #1c0522;
  min-height: 100vh;
}

.sec p {
  font-size: 1.5em;
  color: #fff;
  margin-top: 40px;
}

.welcome-text {
  margin-top: 0;
  padding: 0 200px 0 100px;
}

.welcome-paragraph {
  font-size: 1.2em;
  color: #fff;
  margin: 30px 0;
  line-height: 1.8;
  text-align: left;
  max-width: 800px;
  margin-left: 0;
  margin-right: auto;
  opacity: 0;
  transform: translateY(30px);
  transition: all 0.8s ease;
}

.welcome-paragraph.animate {
  opacity: 1;
  transform: translateY(0);
}

@keyframes float-up {
  0% {
    opacity: 0;
    transform: translateY(50px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Logo图片样式 */
.logo-img {
  height: 80px;
  width: auto;
  transition: all 0.3s ease;
}

.logo-img:hover {
  transform: scale(1.1);
}

/* 标题容器样式 */
.title-container {
  position: relative;
  font-size: 48px;
  font-weight: 800;
  color: #fff;
  margin-bottom: 50px;
  display: inline-block;
}

.title-container span {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 80px;
  line-height: 80px;
  text-align: center;
  transform: skewY(-15deg);
  transform-origin: left;
}

.title-container span:nth-child(even) {
  transform: skewY(15deg);
  transform-origin: right;
}

.title-container span::after {
  position: absolute;
  content: '';
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background-color: #876DE3;
  z-index: -1;
}

.title-container span:nth-child(even)::after {
  background-color: #563387;
}

.title-container span::before {
  position: absolute;
  content: '';
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background-color: #342B57;
  z-index: -2;
  transform: skewY(15deg);
  transform-origin: left;
}

.title-container span:nth-child(even)::before {
  transform: skewY(-15deg);
  transform-origin: right;
}

/* 鼠标移入时效果 */
.title-container span.flat {
  transform: skewY(0deg);
}

.title-container span.flat:nth-child(even) {
  transform: skewY(0deg);
}

.title-container span.flat::before {
  transform: skewY(0deg);
}

.title-container span.flat:nth-child(even)::before {
  transform: skewY(0deg);
}

.title-container span.flat:nth-child(even)::after {
  background-color: #876DE3;
}

/* 特别定义 */
.title-container span.first::after {
  background-color: #c57052 !important;
}

/* 响应式调整 */
@media (max-width: 768px) {
  header {
    padding: 20px;
  }
  
  section {
    padding: 20px;
  }
  
  #text {
    font-size: 8vw;
  }
  
  .sec {
    padding: 40px 20px;
  }
  
  .welcome-text {
    padding: 0 150px 0 20px;
  }
  
  .title-container {
    font-size: 32px;
  }
  
  .title-container span {
    width: 40px;
    height: 60px;
    line-height: 60px;
  }
}

@media (max-width: 480px) {
  .title-container {
    font-size: 24px;
  }
  
  .title-container span {
    width: 30px;
    height: 45px;
    line-height: 45px;
  }
  
  .sec p {
    font-size: 1.2em;
  }
  
  .welcome-text {
    padding: 0 100px 0 20px;
  }
}

/* 花朵组容器样式 */
.flowers-group {
  position: absolute;
  right: 200px;
  top: 50%;
  transform: translateY(-50%);
  opacity: 0;
  transform: translateY(-50%) scale(0.3) rotate(-180deg);
  transition: all 1.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 20px;
  align-items: center;
}

.flowers-group.show {
  opacity: 1;
  transform: translateY(-50%) scale(1) rotate(0deg);
}

/* Logo GIF 容器样式 */
.logo-gif-container {
  opacity: 0;
  transform: scale(0.5) translateY(20px);
  transition: all 1.2s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  margin-top: 500px;
}

.logo-gif-container.show {
  opacity: 1;
  transform: scale(1) translateY(0);
}

/* Logo GIF 样式 */
.logo-gif {
  width: 240px;
  height: auto;
  border-radius: 15px;
  filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.4));
  animation: logoFloat 3s ease-in-out infinite;
}

/* Logo 浮动动画 */
@keyframes logoFloat {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-8px) rotate(2deg);
  }
}

/* 花朵主体样式 */
.flower {
  position: relative;
  top: -80px;
  left: -20px;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.3));
}

/* 三朵花的不同样式 */
.flower:nth-child(1) {
  transform: scale(1);
}

.flower:nth-child(2) {
  transform: scale(0.7);
  left: -80px;
  top: -19px;
}

.flower:nth-child(3) {
  transform: scale(0.7);
  left: 55px;
  top: -19px;
}

/* 花瓣样式 */
.flower .petal {
  position: absolute;
  top: 0;
  left: 0;
  background-color: #d52d58;
  border-radius: 0px 30px 0px 30px;
  width: 30px;
  height: 60px;
}

.flower .petal:nth-child(1) {
  left: -10px;
  transform: rotate(-10deg);
  transform-origin: bottom right;
  background-color: #b81b43;
  animation: openleft 3s ease-in infinite;
  z-index: 5;
}

.flower .petal:nth-child(2) {
  left: -10px;
  background-color: #c9204b;
  z-index: 4;
}

.flower .petal:nth-child(3) {
  border-radius: 30px;
  left: 5px;
}

.flower .petal:nth-child(4) {
  border-radius: 30px 0px 30px 0px;
  background-color: #c9204b;
  left: 20px;
  z-index: 4;
}

.flower .petal:nth-child(5) {
  border-radius: 30px 0px 30px 0px;
  left: 20px;
  transform: rotate(10deg);
  transform-origin: bottom left;
  animation: openright 3s ease-in infinite;
  background-color: #b81b43;
  z-index: 5;
}

/* 花茎样式 */
.flower .rod {
  background-color: #329932;
  width: 5px;
  height: 150px;
  position: absolute;
  left: 17.5px;
  top: 58px;
  z-index: -1;
  animation: stemSway 3s ease-in-out infinite;
  transform-origin: top;
}

.flower .rod::before {
  background-color: lightgreen;
  border-radius: 3px;
  content: '';
  display: block;
  width: 20px;
  height: 8px;
  margin-left: -8px;
}

/* 花茎上的刺 */
.flower .rod .spike {
  width: 0;
  height: 0;
  position: absolute;
  top: 20px;
  left: -10px;
  border-color: transparent #329932 transparent transparent;
  border-style: solid;
  border-width: 5px;
}

.flower .rod .spike:nth-child(2) {
  top: 50px;
  left: 4px;
  border-color: transparent transparent transparent #329932;
}

.flower .rod .spike:nth-child(3) {
  top: 80px;
}

.flower .rod .spike:nth-child(4) {
  top: 110px;
  left: 4px;
  border-color: transparent transparent transparent #329932;
}

/* 关键帧动画 */
@keyframes openleft {
  20%, 80% {
    transform: rotate(-20deg);
  }
}

@keyframes openright {
  20%, 80% {
    transform: rotate(20deg);
  }
}

@keyframes stemSway {
  0%, 100% {
    transform: rotate(0deg);
  }
  25% {
    transform: rotate(1deg);
  }
  75% {
    transform: rotate(-1deg);
  }
}

/* 响应式调整 */
@media (max-width: 1200px) {
  .flowers-group {
    right: 150px;
  }
  
  .flower {
    transform: scale(0.8);
  }
  
  .flower:nth-child(2) {
    transform: scale(0.6);
  }
  
  .flower:nth-child(3) {
    transform: scale(0.6);
  }
  
  .logo-gif {
    width: 200px;
  }
}

@media (max-width: 768px) {
  .flowers-group {
    position: relative;
    right: auto;
    top: auto;
    transform: none;
    margin: 30px auto;
    display: flex;
    justify-content: center;
    flex-direction: row;
    gap: 10px;
  }
  
  .flowers-group.show {
    transform: scale(1) rotate(0deg);
  }
  
  .flower {
    transform: scale(0.5);
  }
  
  .flower:nth-child(2) {
    transform: scale(0.35);
  }
  
  .flower:nth-child(3) {
    transform: scale(0.35);
  }
  
  .logo-gif {
    width: 160px;
  }
}
</style> 